package com.michale.common.domain.security;

import com.alibaba.fastjson2.annotation.JSONField;
import com.michale.common.domain.permissions.UserEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Michale
 * @apiNote 登录用户信息
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class LoginUserDetail implements UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("用户ID")
    private Long userId;

    @ApiModelProperty("部门ID")
    private Long deptId;

    @ApiModelProperty("用户唯一标识")
    private String token;

    @ApiModelProperty("登录时间")
    private Date loginTime;

    @ApiModelProperty("token的过期时间")
    private Long expireTime;

    @ApiModelProperty("登录IP地址")
    private String ipAddress;

    @ApiModelProperty("登录地点")
    private String loginLocation;

    @ApiModelProperty("浏览器类型")
    private String browser;

    @ApiModelProperty("操作系统")
    private String os;

    @ApiModelProperty("权限列表")
    private Set<String> permissions;

    @ApiModelProperty("用户信息")
    private UserEntity userEntity;

    public LoginUserDetail(Long userId, Long deptId, UserEntity userEntity, Set<String> permissions) {
        this.userId = userId;
        this.deptId = deptId;
        this.permissions = permissions;
        this.userEntity = userEntity;
    }

    public LoginUserDetail(Long userId, Set<String> permissions) {
        this.userId = userId;
        this.permissions = permissions;
    }

    /**
     * 授予用户权限
     *
     * @return 封装用户权限信息
     */
    @JSONField(serialize = false)
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return userEntity.getPassword();
    }

    @Override
    public String getUsername() {
        return userEntity.getUserName();
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }
}
